const app = getApp()
var http = require('../../../utils/server.js')
var util = require('../../../utils/util.js')

Page({
  data: {
    specifications: []
  },
  onLoad: function(option) {
    var that = this
    that.setData({
      product_id: option.product_id
    })
    http.post({
      url: "product/getDetail",
      product_id: that.data.product_id
    }, function(data) {
      console.log('商品规格', data.specifications)
      var defaultPrice = []
      for (var i in data.specifications) {
        defaultPrice.push({
          'gang_price': data.specifications[i]['gang_price'],
          'retail_price': data.specifications[i]['retail_price']
        })
      }
      that.setData({
        specifications: data.specifications,
        defaultPrice: defaultPrice,
        tag_price: data.tag_price
      })
    })

  },

  bindEditPrice(e) {
    var index = e.currentTarget.dataset.index
    var stype = e.currentTarget.dataset.type
    var specifications = this.data.specifications
    specifications[index][stype] = e.detail.value
    this.setData({
      specifications: specifications
    })
  }, 
  sub: function (e) {
    var index = e.currentTarget.dataset.index
    var stype = e.currentTarget.dataset.type
    var specifications = this.data.specifications
    var defaultPrice = this.data.defaultPrice
    console.log(stype)
    if (typeof (specifications[index][stype]) == "undefined")
      specifications[index][stype] = 0
    specifications[index][stype]--
    var lowerLimit = 1
    if (stype == 'gang_price' || stype == 'retail_price') {
      lowerLimit = defaultPrice[index][stype]
    }
    if (specifications[index][stype] <= lowerLimit) {
      specifications[index][stype] = lowerLimit
      specifications[index][stype + "_disabled"] = true
    }
    specifications[index][stype] = Math.round(specifications[index][stype] * 100) / 100
    this.setData({
      specifications: specifications
    })
  },
  add: function (e) {
    var index = e.currentTarget.dataset.index
    var stype = e.currentTarget.dataset.type
    var specifications = this.data.specifications
    if (typeof (specifications[index][stype]) == "undefined")
      specifications[index][stype] = 0
    specifications[index][stype]++
    specifications[index][stype + "_disabled"] = false
    specifications[index][stype] = Math.round(specifications[index][stype] * 100) / 100
    this.setData({
      specifications: specifications
    })
  },
  isPrice(val) {
    var regPos = /^\d+(\.\d+)?$/; //非负浮点数
    if (regPos.test(val)) {
      return true;
    } else {
      return false;
    }
  },
  save() {
    var that = this
    var specifications = this.data.specifications
    for (s of specifications) {
      if (!this.isPrice(s.retail_price)) {
        util.confirmModal('请正确输入' + s.title + '的零售价格!')
        return false
      }
    }

    http.post({
      url: "product/editPrice",
      product_id: that.data.product_id,
      specifications: that.data.specifications
    }, function(data) {
      util.showSuccess('修改成功', function() {
        wx.navigateBack({
          delta: 1
        })
      })
    })

  }

})